package com.tanhua.dubbo.server.api;


import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.Moments;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service(version = "1.0.0")
public class ManageMomentsApiImpl implements ManageMomentsApi {
	
	
	@Autowired
	private MongoTemplate mongoTemplate;
	
	
	@Override
	public PageInfo<Publish> queryMomentsList(Integer page, Integer pageSize, String sortProp, String sortOrder) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(null).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		// 用户发布的动态初始状态应该都为待审核状态
		List<Publish> publishes = this.mongoTemplate.find(query, Publish.class);
		PageInfo<Publish> pageInfo = new PageInfo<>();
		pageInfo.setRecords(publishes);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(publishes.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Comment> queryCommentsListById(String messageID, Integer page, Integer pageSize, String sortProp, String sortOrder) {
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		Query query = Query.query(Criteria.where("publishId").is(messageID)).with(pageRequest);
		List<Comment> comments = this.mongoTemplate.find(query, Comment.class);
		PageInfo<Comment> pageInfo = new PageInfo<>();
		pageInfo.setRecords(comments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(comments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListReview(Integer page, Integer pageSize, String sortProp, String sortOrder) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").is("review")).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListAll(Integer page, Integer pageSize, String sortProp, String sortOrder) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("review", "auto_pass", "auto_reject", "pass", "reject")).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListReject(Integer page, Integer pageSize, String sortProp, String sortOrder) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_reject", "reject")).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListPass(Integer page, Integer pageSize, String sortProp, String sortOrder) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_pass", "pass")).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListAllByUid(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("review", "auto_pass", "auto_reject", "pass", "reject").and("userId").is(uid)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListReviewByUid(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("review").and("userId").is(uid)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListRejectByUid(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_reject", "reject").and("userId").is(uid)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListPassByUid(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_pass", "pass").and("userId").is(uid)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListAllByUidAndData(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("review", "auto_pass", "auto_reject", "pass", "reject").and("userId").is(uid).and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListReviewByUidAndData(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("review").and("userId").is(uid).and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListRejectByUidAndData(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_reject", "reject").and("userId").is(uid).and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListPassByUidAndData(Integer page, Integer pageSize, String sortProp, String sortOrder, String uid, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_pass", "pass").and("userId").is(uid).and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListAllByData(Integer page, Integer pageSize, String sortProp, String sortOrder, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("review", "auto_pass", "auto_reject", "pass", "reject").and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListReviewByData(Integer page, Integer pageSize, String sortProp, String sortOrder, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("review").and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListRejectByData(Integer page, Integer pageSize, String sortProp, String sortOrder, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_reject", "reject").and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public PageInfo<Moments> queryMomentsListPassByData(Integer page, Integer pageSize, String sortProp, String sortOrder, Long start, Long end) {
		// 设置分页相关信息
		PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("createDate")));
		// 不设置查询条件,按照分页条件查询所有
		Query query = Query.query(Criteria.where("state").in("auto_pass", "pass").and("createDate").lte(end).gte(start)).with(pageRequest);
		// 从MongoDB中获取到所有发布的动态
		List<Moments> moments = this.mongoTemplate.find(query, Moments.class);
		PageInfo<Moments> pageInfo = new PageInfo<>();
		pageInfo.setRecords(moments);
		pageInfo.setPageNum(page);
		pageInfo.setPageSize(pageSize);
		pageInfo.setTotal(moments.size());
		return pageInfo;
	}
	
	@Override
	public Moments findByPid(String id) {
		// 设置查询条件,动态id相等
		Query query = Query.query(Criteria.where("oid").is(id));
		Moments moments = this.mongoTemplate.findOne(query, Moments.class);
		return moments;
	}
	
	@Override
	public void passByPid(String id) {
		Query query = Query.query(Criteria.where("oid").is(id));
		Update update = Update.update("state","pass");
		this.mongoTemplate.updateFirst(query,update,Moments.class);
	}
	
	@Override
	public void RejectByPid(String id) {
		Query query = Query.query(Criteria.where("oid").is(id));
		Update update = Update.update("state","reject");
		this.mongoTemplate.updateFirst(query,update,Moments.class);
	}
	
	@Override
	public void RevocationByPid(String id) {
		Query query = Query.query(Criteria.where("oid").is(id));
		Update update = Update.update("state","review");
		this.mongoTemplate.updateFirst(query,update,Moments.class);
	}
}
